package com.vip.jwt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vip.jwt.common.exception.MyException;
import com.vip.jwt.entity.*;
import com.vip.jwt.mapper.UserMapper;
import com.vip.jwt.mapper.UserRoleMapper;
import com.vip.jwt.service.UserRoleService;
import com.vip.jwt.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserRoleService userRoleService;

    @Override
    public int saveUser(User user) {
        String username = user.getUsername();
        User findUser = findByUsername(username);
        boolean aNull = ObjectUtil.isNotNull(findUser);
        if(aNull){
           throw new MyException("用户名已存在");
        }
        String salt = RandomUtil.randomString(5);
        String password = user.getPassword();
        String encryptPassword = User.encryptPassword(password,salt);
        user.setPassword(encryptPassword);
        user.setSalt(salt);
        userMapper.insert(user);
        List<Role> roles = user.getRoles();
        if(roles.size()>0){
            List<String> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
            roleIds.forEach(e->{
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getId());
                userRole.setRoleId(e);
                userRoleMapper.insert(userRole);
            });
        }
        return 0;
    }

    @Override
    public int deleteById(String id) {
        userMapper.deleteById(id);
        userRoleService.deleteByUserId(id);
        return 0;
    }

    @Override
    public int updateUser(User user) {
        user.setUsername(null);
        user.setPassword(null);
        user.setSalt(null);
        userMapper.updateById(user);
        //删除角色菜单中间表
        userRoleService.deleteByUserId(user.getId());
        List<Role> roles = user.getRoles();
        if (roles.size() > 0) {
            //更新角色菜单中间表
            List<String> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
            roleIds.forEach(e -> {
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getId());
                userRole.setRoleId(e);
                userRoleMapper.insert(userRole);
            });
        }
        return 0;
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        return userMapper.selectOne(userQueryWrapper);
    }

}
